package net.huashitong.Indicators.dao;

import java.text.ParseException;
import java.util.List;

import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.filter.CompletionClassificationFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.DateForIndUtils;

import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.StringUtils;

@Repository
public class CompletionClassificationDao extends StringDao<CompletionClassification> {

	public List<CompletionClassification> queryByfilter(
			CompletionClassificationFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getCategory());
		query.append(" and obj.categoryDetails = :categoryDetails").setParam("categoryDetails", filter.getCategoryDetails());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		try {
			query.append(" and (obj.year = :year or year = :lastyear)").setParam("year", filter.getYear()).setParam("lastyear",DateForIndUtils.getNextYearDate(filter.getYear(), -1));
			query.append(" and obj.completionPeriod <= :completionPeriod").setParam("completionPeriod", filter.getCompletionPeriod());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		query.getOrders().put("completionPeriod", BaseFilter.DESC);
		return query.listResult();
	}

	public List<CompletionClassification> queryByValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
	    query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getBm());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod = :completionPeriod ORDER BY CAST(obj.planneValue as int) desc ").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryByXZValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getXz());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod = :completionPeriod ").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryByHYValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getHy());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod like :completionPeriod ").setParam("completionPeriod", StringUtils.getLikewords(filter.getDate().substring(0, 4)));
		try {
			query.append(" and obj.completionPeriod < :month order by obj.completionPeriod ASC").setParam("month", DateForIndUtils.getNextMonthValueDate(filter.getDate(), 1));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return query.listResult();
	}

	public List<CompletionClassification> queryByDWValue(
			IndicatorBreakdownFilter filter) {
		
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getQy());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod = :completionPeriod order by cast(planneValue as int) desc").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryByDWYearValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getQy());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.year = :year order by cast(actualCompletionValue as int) desc").setParam("year", filter.getDate().substring(0,4));
		return query.listResult();
	}

	public List<CompletionClassification> queryByHYValueSec(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getHy());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod like :completionPeriod ORDER BY CAST(obj.planneValue as int) DESC ").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryMonthValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getHy());
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod = :completionPeriod").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryCountyPreemptive(
			CompletionClassificationFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getCategory());
		query.append(" and obj.year = :year ORDER BY CAST(obj.actualCompletionValue as int) DESC").setParam("year", filter.getYear());
		return query.listResult();
	}

	public List<CompletionClassification> queryByThiValue(
			IndicatorBreakdownFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getBm());
		if(StringUtils.isNotBlank(filter.getCaDetails())){
			query.append(" and obj.categoryDetails = :categoryDetails").setParam("categoryDetails", filter.getCaDetails());
		}
		query.append(" and obj.area = :area").setParam("area", filter.getArea());
		query.append(" and obj.completionPeriod = :completionPeriod ").setParam("completionPeriod", filter.getDate());
		return query.listResult();
	}

	public List<CompletionClassification> queryPreemptive(
			CompletionClassificationFilter filter) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.industryId = :industryId").setParam("industryId", filter.getIndustryId());
		query.append(" and obj.category = :category").setParam("category", filter.getCategory());
		query.append(" and obj.completionPeriod = :completionPeriod order by cast(obj.actualCompletionValue as int) ASC").setParam("completionPeriod", filter.getCompletionPeriod());
		return query.listResult();
	}

}
